import util from "./util.js"
import strUtil from "./strUtil.js"

import CONFIG from "./constant.js"

function Core() {

}


Core.prototype.choose_image_upload = function(imgNum, callback) {
	let img_list = [];

	uni.chooseImage({
		sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
		sourceType: ['album'], //从相册选择
		count: imgNum,
		success: (res) => {
			img_list = img_list.concat(res.tempFilePaths);
			requestUtil.upload_img(img_list, callback)
		}
	})

}
Core.prototype.upload_img = function(img_list, callback) {
		let LIST = img_list;
		let images = [];
		for (var i = 0, len = LIST.length; i < len; i++) {
			var image_obj = {
				name: 'image-' + i,
				uri: LIST[i]
			};
			images.push(image_obj);
		}
		var uploadNum = 0;
		var imgList = [];
		console.log(images.length + "===上传文件的个数");
		uni.showLoading({
			"title": "上传中"
		});
		for (let i in images) {
			let file = images[i].uri;
			requestUtil.upLoad(file, (uploadFileRes) => {
				let obj = JSON.parse(uploadFileRes);
				let img = obj.data[0].url
				imgList.push(img);
				uploadNum++;
			}, (err) => {

			})
		}
		var i = setInterval(() => {
			if (uploadNum == images.length) {
				uni.hideLoading()
				callback(imgList);
				clearInterval(i);
			}
		}, 2000)
	},
	Core.prototype.upLoad = function(file, onSuccess, onError) { //用于base64图片的上传
		return new Promise((resolve, reject) => {
			uni.uploadFile({
				url: CONFIG.uploadUrl, //仅为示例，非真实的接口地址
				filePath: file,
				name: 'file',
				formData: {
					'user': 'test'
				},
				success: (res) => {
					if (typeof onSuccess == "function") {
						onSuccess(res.data)
					}
					resolve(res.data)
				},
				fail: (res) => {
					if (typeof onError == "function") {
						onError(res.data)
					}
					onError(res.data)
				}
			});
		})
	}

Core.prototype.post = function(url, data, onSuccess, onError) {
	return new Promise((resolve, reject) => {
		var conf = {};
		if (url.indexOf("http") == -1) {
			url = CONFIG.API + url;
		}
		conf.url = url
		if (data) {
			conf.data = data
		}
		conf.success = (res) => {
			//console.log(res);
			res.data.isOk = () => {
				return res.data.status == 200
			}
			if (!res.data.isOk()) {
				if (typeof onError == "function") {
					onError(res.data)
				}
				reject(res.data)
				return

			}
			if (typeof onSuccess == "function") {
				onSuccess(res.data)
			}
			resolve(res.data)
		};
		conf.fail = (res) => {

			if (typeof onError == "function") {
				onError(res.data)
			}
			reject(res.data)
		};

		conf.header = {
			"Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
			"clientplatform": "xapp"
		}



		let token = util.data_local("token");
		if (strUtil.notNull(token)) {
			conf.header["M-Token"] = token;
		}

		conf.method = "POST";
		return uni.request(conf)
	})

};

var requestUtil = new Core();
export default requestUtil
